package com.myqm.controller.weixin;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.myqm.dto.ResultResponse;
import com.myqm.pojo.weixin.Car;
import com.myqm.pojo.weixin.GoodsInfo;
import com.myqm.pojo.weixin.User;
import com.myqm.pojo.weixin.query.QueryCar;
import com.myqm.pojo.weixin.query.QueryUser;
import com.myqm.service.weixin.CarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "购物车")
@RestController
@RequestMapping(value = "/weixin/car")
public class CarController extends BaseController{

	@Autowired
	private CarService carService;
	
	@ApiOperation(value="清空所有购物车数据")
	@DeleteMapping(value="/delAll")
	public ResultResponse<Integer> delById() {
		Integer customerId = this.getSelfCustomerId();
		return new ResultResponse<Integer>(carService.delById(customerId));
	}
	
	@ApiOperation(value="删除购物车记录")
	@DeleteMapping(value="/delgoods")
	public ResultResponse<Integer> del(@ApiParam(required=true,value="货物信息") 
				@RequestBody GoodsInfo goodsInfo){
		goodsInfo.setCustomerId(this.getSelfCustomerId()); 
		return new ResultResponse<Integer>(carService.del(goodsInfo));
	}
	
	@ApiOperation(value="新增记录")
	@PostMapping(value="/add")
	@ApiParam(required = true, value = "记录值")
	public ResultResponse<Integer> save(@Valid  @RequestBody Car record) {
		record.setCustomerId(this.getSelfCustomerId()); 
		return new ResultResponse<Integer>(carService.add(record));
	}
	
	@ApiParam(required = true , value = "记录内容")
	@PutMapping(value = "/edit") 
	public ResultResponse<Integer> editById(@Valid @RequestBody Car record) {
		record.setCustomerId(this.getSelfCustomerId()); 
		return new ResultResponse<Integer>(carService.editById(record));
	}
	
	@ApiOperation(value = "用户id列表查询(分页)") 
	@PostMapping("/list")
	public ResultResponse<PageInfo<Car>> listByCustomerId(@ApiParam(required = false, value = "用户id")
			@Valid @RequestBody QueryCar param){
		return new ResultResponse<PageInfo<Car>>(
				carService.listByCustomerId(param.getPageNum(), param.getPageSize(),param));
	}
	
	@ApiOperation(value = "用户id列表查询(不分页)") 
	@PostMapping("/listAll")
	public ResultResponse<List<Car>> listAll(@ApiParam(required = false, value = "用户id")
			@Valid @RequestBody QueryCar param){
		return new ResultResponse<List<Car>>(
				carService.listAll(param));
	}
	
	
}
